﻿Imports System.Data.OleDb
Public Class frmDVT
    Inherits DevComponents.DotNetBar.Office2007Form
    Private Sub frmQuyCach_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DVTSanPham_Load()
        'Phân quyền Form
        Dim aPermission(4) As Boolean
        aPermission = PhanQuyenForm("MenuDonViTinh", frmMain.UserId)
        cmdAddNew.Enabled = aPermission(0)
        cmdEdit.Enabled = aPermission(1)
        cmdDelete.Enabled = aPermission(2)
    End Sub
#Region "Load Data"
    REM Load DonViTinh_SanPham
    Private Sub DVTSanPham_Load()
        Dim dbConn As New OleDb.OleDbConnection
        Dim dataAccess As New DataAccess
        Dim dtDVTSanPham As New DataTable
        Dim query As String = "Select DVTSanPhamId,TenDonViTinh,SoLuong,TenDonVi,TenQuyCachDongGoi,DVTSanPham.GhiChu From DVTSanPham " & _
                                " Left Outer Join DonViTinh ON DVTSanPham.IDDonViTinh = DonViTinh.IdDonVi" & _
                                " Left Outer Join QuyCachDongGoi On QuyCachDongGoi.QuyCachDongGoiId=DVTSanPham.QuyCachDongGoiId" & _
                                " Where(DVTSanPham.Xoa = 0)"
        Dim adapter As OleDb.OleDbDataAdapter
        dataAccess.ConnectDatabase_DBAccess(dbConn)
        adapter = New OleDb.OleDbDataAdapter(query, dbConn)
        adapter.Fill(dtDVTSanPham)
        dbConn.Close()
        dtDVTSanPham.Columns.Add("STT")
        For i As Integer = 0 To dtDVTSanPham.Rows.Count - 1
            dtDVTSanPham.Rows(i)("STT") = i + 1
        Next
        dgvQuyCach.AutoGenerateColumns = False
        dgvQuyCach.DataSource = dtDVTSanPham
    End Sub

#End Region
#Region " Add, Edit, Delete"
    Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
        Try
            ShowModalForm(frmThemDVT, False, False)
            DVTSanPham_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            frmThemDVT.DVTSanPhamId = Integer.Parse(dgvQuyCach.Rows(dgvQuyCach.CurrentCell.RowIndex).Cells("DVTSanPhamId").Value.ToString)
            ShowModalForm(frmThemDVT, False, False)
            DVTSanPham_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            Dim DVTSanPhamId As Integer = Integer.Parse(dgvQuyCach.Rows(dgvQuyCach.CurrentCell.RowIndex).Cells("DVTSanPhamId").Value.ToString)
            Dim TenDonViTinh As String = dgvQuyCach.Rows(dgvQuyCach.CurrentCell.RowIndex).Cells("TenDonViTinh").Value.ToString
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa  : " & TenDonViTinh, _
                  "Thông báo", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                'Update
                Dim connectAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                Dim dbCommand As OleDbCommand
                Dim query As String = "Update DVTSanPham Set Xoa=1,NguoiXoa='" & frmMain.UserName & "',NgayXoa='" & Format(Now, "dd/MM/yyy HH:mm:ss") & "' Where DVTSanPhamId=" & DVTSanPhamId
                connectAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                DVTSanPham_Load()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try

        Catch ex As Exception

        End Try
    End Sub
#End Region
  

    
End Class